package com.ac.api.component.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ac.api.component.entity.CpArticle;
import com.ac.api.component.dao.CpArticleMapper;
import com.ac.api.component.entity.CpLabel;
import com.ac.api.component.service.CpArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ac.api.component.service.CpLabelService;
import com.ac.api.component.vo.CpArticleVo;
import com.ac.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gjj
 * @since 2024-10-26
 */
@Service
public class CpArticleServiceImpl extends ServiceImpl<CpArticleMapper, CpArticle> implements CpArticleService {
    @Autowired
    private CpLabelService cpLabelService;
    @Override
    public CpLabel findLabelByName(String name) {
        CpLabel cpLabel =cpLabelService.lambdaQuery().eq(CpLabel::getName, name).one();
        return cpLabel;
    }

    @Override
    public List<CpArticle> list(CpArticle example) {
        return this.list(buildWrapper(example));
    }

    @Override
    public IPage<CpArticle> page(CpArticle example, IPage page) {
        return this.page(page,buildWrapper(example));
    }

    /**
     * 构建查询
     *
     * @param example
     * @return
     */
    private QueryWrapper<CpArticle> buildWrapper(CpArticle example) {
        QueryWrapper<CpArticle> wrapper     = new QueryWrapper<>();
        return wrapper;
    }

    /**
     * 构建查询
     *
     * @param example
     * @return
     */
    private QueryWrapper<CpArticleVo> buildCpArticleVoWrapper(CpArticleVo example) {
        QueryWrapper<CpArticleVo> wrapper     = new QueryWrapper<>();
        wrapper.like(StringUtil.isNotBlank(example.getTitle()),"title",example.getTitle())
                .eq(StringUtil.isNotBlank(example.getModuleName()),"module_name",example.getModuleName())
                .eq(StringUtil.isNotBlank(example.getLabelName()),"label_name",example.getLabelName())
                .eq("state","t")
                .orderByDesc("top")
                .orderByDesc("createtime");
        return wrapper;
    }

    @Override
    public Page<CpArticleVo> findByPage(CpArticleVo example, Page page) {
        page.setOptimizeCountSql(false);
        return page.setRecords(this.baseMapper.findByExample(buildCpArticleVoWrapper(example), page));
    }

}
